package com.sskj.run.ui.fragment;


import android.support.v4.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.rxbus.MainTabChange;
import com.sskj.lib.bean.TabItem;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.run.R;
import com.sskj.run.R2;
import com.sskj.run.bean.StockBloomBean;
import com.sskj.run.component.DaggerUserDataComponent;
import com.sskj.run.presenter.RunFragmentPresenter;

import java.util.ArrayList;
import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.RUN_FRAGMENT_MAIN)
public class RunFragment extends BaseFragment<RunFragmentPresenter> {
    @BindView(R2.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    private UserData userData;
    private ArrayList<Fragment> fragments;
    HashMap<Integer, Fragment> tabMap = new HashMap<>();
    private int curPos;

    @Override
    protected int getLayoutId() {
        return R.layout.run_fragment_main;
    }

    @Inject
    UserViewModel userViewModel;

    @Override
    public RunFragmentPresenter getPresenter() {
        return new RunFragmentPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        RxBus.getDefault().register(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        initTab();
    }
    public void send(StockBloomBean stockBloomBean){
        LiveDataBus.get().with(RxBusCode.BOOM_PUSH, StockBloomBean.class)
                .postValue(stockBloomBean);
    }

    @Override
    protected void initData() {
        mPresenter.initWebSocket();
    }

    private void initTab() {
        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.run_runFragment1), R.mipmap.run_icon_my_guess_unselect, R.mipmap.run_icon_my_guess));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.run_runFragment2), R.mipmap.run_icon_tab_rank_unselect, R.mipmap.run_icon_tab_rank));
        tabItems.add(new TabItem("FAQ", R.mipmap.run_icon_question_unselect, R.mipmap.run_icon_question));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.run_runFragment3), R.mipmap.run_icon_asset_unselect, R.mipmap.run_icon_asset));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.run_runFragment4), R.mipmap.run_icon_honor_unselect, R.mipmap.run_icon_honor));
        commonTabLayout.setTabData(tabItems);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position > 2) {
                    if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                        commonTabLayout.setCurrentTab(curPos);
                        return;
                    }
                }
                changeTab(position);

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        fragments = new ArrayList<>();
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.RUN_FRAGMENT_QIANGJIN).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.RUN_FRAGMENT_RANK).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.RUN_FRAGMENT_FAQ).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.RUN_FRAGMENT_ASSET).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.RUN_FRAGMENT_USER_INFO).navigation());
        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.frameLayout, fragments.get(0))
                .show(fragments.get(0))
                .commitAllowingStateLoss();
        tabMap.put(0, fragments.get(0));

    }

    private void changeTab(int position) {
        if (tabMap.get(position) == null) {
            getChildFragmentManager()
                    .beginTransaction()
                    .add(R.id.frameLayout, fragments.get(position))
                    .hide(fragments.get(curPos))
                    .show(fragments.get(position))
                    .commitAllowingStateLoss();
            tabMap.put(position, fragments.get(position));
        } else {
            getChildFragmentManager()
                    .beginTransaction()
                    .hide(fragments.get(curPos))
                    .show(fragments.get(position))
                    .commitAllowingStateLoss();
        }
        RxBus.getDefault().post(new MainTabChange(position));
        curPos = position;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, code = RxBusCode.CHANGE_RUN_ASSET)
    public void changeAsset() {
        commonTabLayout.setCurrentTab(3);
        changeTab(3);
    }

    @Override
    public void onDestroy() {
        mPresenter.closeWebSocket();
        super.onDestroy();
    }
}
